/*============================================================================
 Project     : suppoker
 Module      : card.c
 Author      : Pierre-Arthur MATHIEU 93482
 Version     : 0.0
 Description : B1 dev project
 ============================================================================
*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "card.h"


int randomCard(int a, int b) //random entre a et b a<b
    {
    return rand()%(b-a) +a;
    }

void putCard(card deck[53],card hand[4],int indiceHand,int indiceDeck)
    {
    if (deck[indiceDeck].selected==1)
        {
        putCard(deck,hand,indiceHand,randomCard(1,53));
        }
    else
        {
        deck[indiceDeck].selected=1;
        hand[indiceHand]=deck[indiceDeck];
        }
    }

void clearDeck(card deck[53])
    {

	int i, j;
	int k;

    deck[0].color=0;
    deck[0].value=0;
    deck[0].selected=0;


    j=1;
    for (k=1; k <= 4; k++)
        {
        for (i = 1; i <= 13; i ++)
            {
            deck[j].value = i;
            j++;
            }
        }
    j=1;
    for (k=1; k <= 4; k++)
        {
        for (i=1; i <= 13; i++)
            {
            deck[j].color = k;
            j++;
            }
        }

    for (k= 0; k <= 52; k++)
        {
        deck[k].selected = 0;
        }
    }
void printDeck(card deck[53])
    {
    int j;
    printf("\nDeck:\n");
    for(j = 0; j <= 52; j++)
        {
        printf("%d %d %d \n", deck[j].value, deck[j].color, deck[j].selected);
        }
    }

void printHand(card hand[4])
    {
    int j;
    printf("\nHand:\n");
    for(j = 0; j <= 4; j++)
        {
        switch (hand[j].value)
            {
            case 1:
                printf("as");
                break;
            case 11:
                printf("valet");
                break;
            case 12:
                printf("dame");
                break;
            case 13:
                printf("roi");
                break;
            default:
                printf("%d",hand[j].value);
                break;
            }
        switch (hand[j].color)
            {
            case 1:
                printf(" de coeur\n");
                break;
            case 2:
                printf(" de carreau\n");
                break;
            case 3:
                printf(" de pique\n");
                break;
            case 4:
                printf(" de treffle\n");
                break;
             }
        }
    }

void handSort(card hand[4]) //methode du tri a bulle
    {
    int size = 5;
    int unsorted = 1;
    int i,j;
    for (i = 0; (i < size) && unsorted; ++i)
        {
        unsorted = 0;
        for (j = 1; j < (size - i); ++j)
            if (hand[j-1].value > hand[j].value)
                {
                card tmp = hand[j-1];
                hand[j-1] = hand[j];
                hand[j] = tmp;
                unsorted = 1;
                }
        }
    }